#include "gwwdl.hpp"

const int  ciWidth					= 300;										// width of WndMain
const int  ciHeight					= 300;										// height of WndMain

//HWND g_hwndMain;

int MyRegisterClass ( HINSTANCE hInstance ) {

	WNDCLASSEX	wcex;															// Window Class
	
	wcex.cbSize 		= sizeof( WNDCLASSEX );									// struct Size (required)
	wcex.style			= CS_HREDRAW | CS_VREDRAW;								// dbl check create styles
	wcex.lpfnWndProc	= WndMainProcedure;										// WinProc Pointer
	wcex.cbClsExtra		= 0;													// don't worry this yet
	wcex.cbWndExtra		= 0;													//		or this
	wcex.hInstance		= hInstance;											// Instance Handle from Argument
	wcex.hIcon			= LoadIcon( NULL, IDI_APPLICATION );					// Default Icon ( TODO!! )
	wcex.hIconSm		= LoadIcon( NULL, IDI_WINLOGO );						// Default Small Icon ( TODO!! )
	wcex.hCursor		= LoadCursor( NULL, IDC_ARROW );						// Default Arrow Cursor
	wcex.hbrBackground	= (HBRUSH) COLOR_WINDOW;								// Use System Setting for COLOR_WINDOW
	wcex.lpszMenuName	= MAKEINTRESOURCE( IDM_DLMAIN );						// load menu resource
	wcex.lpszClassName  = cszWndMainClass;										// use our const string
	
	if( !RegisterClassEx( &wcex ) ) return GW_FAIL_REGISTERWINCLASS;			// check for fail, return if so
	
	return GW_SUCCESS;															// return on success
	
}

void MyUnregisterClass( HINSTANCE hInstance ) {

	UnregisterClass( cszWndMainClass, hInstance );

}

HWND MyCreateWindow( HINSTANCE hInstance ) {

	HWND hwndMain = CreateWindowEx( NULL, cszWndMainClass, cszWndMainTitle,		// extra styles, class, title
									WS_OVERLAPPEDWINDOW, 						// application window style
									CW_USEDEFAULT, CW_USEDEFAULT,				// X, Y (let windows decide)
									ciWidth, ciHeight,							// Width and Height (constants)
									HWND_DESKTOP,								// Child of Desktop (root) Window
									NULL,										// Unused/Deprecated Menu Handle
									hInstance,									// Instance Handle Argument
									NULL );										// extra parameters
									
	return hwndMain;
									
//
//	if( !g_hwndMain ) return GW_FAIL_CREATEWNDMAIN;								// verify we made one
//	
//	return GW_SUCCESS;															// return on success

}

int Run( HINSTANCE hInstance ) {

	MSG msg;																	// message struct
	HWND hwndMain;
	
	if( MyRegisterClass( hInstance ) < 0 ) {									// ErrorState is anything < 0
		NotifyFatal( cszFatalNoRegisterWin );
		return GW_FAIL_REGISTERWINCLASS;										
	}
	
//	if( MyCreateWindow( hInstance ) < 0 ) {										// ErrorState is anything < 0
//		NotifyFatal( cszFatalNoCreateWin );
//		MyUnregisterClass( hInstance );											// clean up our mess
//		return GW_FAIL_CREATEWNDMAIN;											
//	}

	hwndMain = MyCreateWindow( hInstance );
	if ( !hwndMain ) {
		NotifyFatal( cszFatalNoCreateWin );
		MyUnregisterClass( hInstance );											// clean up our mess	
	}

	ShowWindow( hwndMain, SW_NORMAL );										// explicity show and
	UpdateWindow( hwndMain );													// update our windwo


	while ( GetMessage( &msg, NULL, 0, 0 ) > 0 ) {								// literally sit and wait for WM's
		TranslateMessage( &msg );												// process for VK's etc
		DispatchMessage( &msg );												// Send It!!!
	}
	
	MyUnregisterClass( hInstance );												// clean up our mess
	
	return static_cast<int>(msg.wParam);

}


void NotifyFatal( const char* szFatalMessage ) {

	if ( szFatalMessage == NULL )												// lazy programmer didn't give msg
		MessageBox( NULL, cszFatalMsg, cszFatalMsgTitle, MB_OK );				// use default from constants
	else
		MessageBox( NULL, szFatalMessage, cszFatalMsgTitle, MB_OK );			// use arguments
		
}

LRESULT CALLBACK WndMainProcedure( HWND hwnd, UINT msg, 
									WPARAM wparam, LPARAM lparam ) {

	switch( msg ) {
	
		case WM_CREATE:
		
			break;
			
			
		case WM_COMMAND:
			switch( wparam ) {
				case ID_ABOUT:
					DialogBox( GetModuleHandle(NULL), MAKEINTRESOURCE( IDD_ABOUT ), 
						hwnd, (DLGPROC)AboutDialogProcedure );
					break;
				case ID_EXIT:
					PostQuitMessage( GW_SUCCESS );
					break;
				default:
					MessageBox( hwnd, "Not Implemented", "Error", MB_OK );
					break;
			}
							
			break;
			
//		case WM_PAINT:
//		{
//			HDC 		hdc;
//			PAINTSTRUCT ps;
			
//			hdc = BeginPaint( hwnd, &ps );
			
//			EndPaint( hwnd, &ps );
																				// begin code block
//		}			
			
		case WM_CLOSE:
		case WM_DESTROY:
			PostQuitMessage( GW_SUCCESS );										// kill message loop
			break;
			
		default:
			return DefWindowProc( hwnd, msg, wparam, lparam );					// default handler
	
	}
	
	return GW_SUCCESS;															// shouldn't ever error here									
									
}

LRESULT CALLBACK AboutDialogProcedure ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam ) {

	switch( message ) {
		case WM_INITDIALOG:
	
				// shit to do when init
	       
			return true;    
		
			break;   

		case WM_COMMAND:
			// SINGLE OK BUTTON

			EndDialog(hwnd, LOWORD(wParam));
			break;


		case WM_CLOSE:

				// system close but not called by enddialog
				// i think i need to mod this

			break;
	    
	}
	    
	return false;
         
}




